package com.moyun.finance.service;

import com.moyun.finance.entity.*;
import com.moyun.finance.repository.*;
import com.moyun.finance.utils.BaseUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ReimbursementDetailedService {
    @Autowired
    ReimbursementRepository rr;
    @Autowired
    ProjectsRepository pr;
    @Autowired
    OrgUserRepository our;
    @Autowired
    BillRepository br;
    @Autowired
    BillService bs;
    @Autowired
    AccountRepository ar;
    @Autowired
    ReimbursementDetailedRepository rdr;
    @Autowired
    CustomerRepository cr;
    @Autowired
    TypeMenuRepository tmr;

    public Response save(List<ReimbursementDetailed> rds, String rId){
        for(ReimbursementDetailed rd : rds){
            rd.setId(BaseUtil.uuid());
            rd.setReimbursementId(rId);
            rdr.save(rd);
        }
        return new Response("添加成功!",true);
    }

    public Response update(List<ReimbursementDetailed> rds){
        for(ReimbursementDetailed rd : rds){
            rdr.save(rd);
        }
        return new Response("修改成功!",true);
    }

    public Response delete(ReimbursementDetailed rd){
        rdr.delete(rd);
        return new Response("删除成功",true);
    }

    public Response findAll(String companyId){
        List<ReimbursementDetailed> rds = rdr.findByCompanyIdOrderByReimbursementDate(companyId);
        for(ReimbursementDetailed rd : rds){
            rd.setCustomer(cr.findById(rd.getCustomerId()).orElse(null));
            rd.setTypeMenu(tmr.findById(rd.getTypeMenuId()).orElse(null));
        }
        return new Response("",rds,true);
    }

    //查找某报销单下的所有报销明细
    public List<ReimbursementDetailed> findByReimbursementId(String reimbursementId){
        List<ReimbursementDetailed> rds = rdr.findByReimbursementIdOrderByDate(reimbursementId);
        for(ReimbursementDetailed rd : rds){
            rd.setCustomer(cr.findById(rd.getCustomerId()).orElse(null));
            rd.setTypeMenu(tmr.findById(rd.getTypeMenuId()).orElse(null));
        }
        return rds;
    }
}
